import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:sensors/sensors.dart';
import 'package:vibration/vibration.dart';

class ShakePage extends StatefulWidget {
  @override
  _ShakePageState createState() => _ShakePageState();
}

class _ShakePageState extends State<ShakePage> {
  bool isShake = false;
  int _currentIndex = 0;

  StreamSubscription _streamSubscription;
  static const int SHAKE_TIMEOUT = 500;
  static const double SHAKE_THRESHOLD = 3.25;
  var _lastTime = 0;

  @override
  void initState() {
    super.initState();

    _streamSubscription = accelerometerEvents.listen((event) {
      var now = DateTime.now().millisecondsSinceEpoch;

      if ((now - _lastTime) > SHAKE_TIMEOUT) {
        var x = event.x;
        var y = event.y;
        var z = event.z;
        double acce = sqrt(x * x + y * y + z * z) - 9.8;
        if (acce > SHAKE_THRESHOLD) {
          print("摇一摇");
          //手机晃动了
          _lastTime = now;
          Vibration.vibrate(); //晃动
          if (!mounted) {
            setState(() {
              isShake = true;
            });
          }
        }
      }
    });
  }

  @override
  void dispose() {
    //// TODO: implement dispose
    super.dispose();
    _streamSubscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("摇一摇"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(Icons.screen_share),
            SizedBox(
              width: 10.0,
            ),
            Text("摇一摇获取礼品")
          ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.folder), title: Text("礼品")),
          BottomNavigationBarItem(
              icon: Icon(Icons.assignment), title: Text("咨询"))
        ],
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}
